package com.xiyu.service.service.files;

import com.xiyu.service.vo.files.housingOwnership.HousingOwnershipSingleGetOutput;
import com.xiyu.service.vo.files.housingOwnership.HousingOwnershipUpdateInput;
import com.xiyu.service.vo.files.housingOwnership.HousingOwnershipCreateInput;
import com.xiyu.service.model.files.certificate.FilesHousingOwnership;
import com.xiyu.service.repository.files.certificate.FilesHousingOwnershipRepository;
import com.xiyu.service.vo.files.housingOwnership.HousingOwnershipPageQueryOutput;
import com.xiyu.service.vo.files.housingOwnership.HousingOwnershipPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.files.HousingOwnershipConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.files.HousingOwnershipErrorCode.*;

/**
 * 房屋所有权证 Service 实现类
 */
@Service
@Validated
public class HousingOwnershipServiceImpl implements HousingOwnershipService {

    @Resource
    private FilesHousingOwnershipRepository filesHousingOwnershipRepository;




    @Override
    public PageResult<HousingOwnershipPageQueryOutput> pageQuery(HousingOwnershipPageQueryInput inputVO) {
        Page<FilesHousingOwnership> pageFilesHousingOwnership = filesHousingOwnershipRepository.pageQuery(inputVO);
        List<HousingOwnershipPageQueryOutput> listFilesHousingOwnership = HousingOwnershipConvert.INSTANCE.pageQueryOutputConvert(pageFilesHousingOwnership.getRows());
        return new PageResult<>(listFilesHousingOwnership, pageFilesHousingOwnership.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(HousingOwnershipCreateInput inputVO) {
        FilesHousingOwnership newFilesHousingOwnership = HousingOwnershipConvert.INSTANCE.createInputConvert(inputVO);
        newFilesHousingOwnership = filesHousingOwnershipRepository.insert(newFilesHousingOwnership);
        return newFilesHousingOwnership.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(HousingOwnershipUpdateInput inputVO) {
        Optional<FilesHousingOwnership> optionalOldFilesHousingOwnership = filesHousingOwnershipRepository.findById(inputVO.getId());
        if(!optionalOldFilesHousingOwnership.isPresent()){
            throw exception(FILES_HOUSING_OWNERSHIP_NOT_EXIST);
        }
        FilesHousingOwnership updateFilesHousingOwnership = HousingOwnershipConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldFilesHousingOwnership.get(), updateFilesHousingOwnership))
            filesHousingOwnershipRepository.update(updateFilesHousingOwnership);
        return true;
    }

    @Override
    public HousingOwnershipSingleGetOutput singleGet(Long id) {
        Optional<FilesHousingOwnership> optionFilesHousingOwnership = filesHousingOwnershipRepository.findById(id);
        if(!optionFilesHousingOwnership.isPresent()){
            throw exception(FILES_HOUSING_OWNERSHIP_NOT_EXIST);
        }
        return HousingOwnershipConvert.INSTANCE.singleGetOutputConvert(optionFilesHousingOwnership.get());

    }

}
